#include "DI8Input.h"

namespace Input_DI {
	HRESULT DIMoInputDevice::Init(HINSTANCE hInstance, HWND hwnd) {
		if(!g_Initialized) {
			DirectInput8Create(	hInstance,
								DIRECTINPUT_VERSION,
								IID_IDirectInput8,
								(void**)&g_DI,
								NULL);
			g_Initialized = true;
		};

		HRESULT hr = g_DI->CreateDevice(GUID_SysMouse,
							  &DIDev,
							  NULL);
		hr = DIDev->SetDataFormat(&c_dfDIMouse);
		hr = DIDev->SetCooperativeLevel(hwnd,
										DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
		
		hr = DIDev->Acquire();

		return hr;
	}

	void DIMoInputDevice::Release(void) {
		if(DIDev != NULL) {
			DIDev->Release();
			DIDev = NULL;
		};
	}

	void DIMoInputDevice::Update(void) {
		DIDev->GetDeviceState(sizeof(State), (LPVOID)&State);
	}

	bool DIMoInputDevice::KeyDown(int Key) {
		return State.rgbButtons[4] == 0x80;
	}

	bool DIMoInputDevice::KeyUp(int Key) {
		return State.rgbButtons[4] != 0x80;
	}

	long DIMoInputDevice::AxisPos(int Axis) {
		switch(Axis) {
		case 0:
			return State.lX;
			break;
		case 1:
			return State.lY;
			break;
		case 2:
			return State.lY;
			break;
		};

		return 0;
	}
};